﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Northwind.NET.Security
{
	[DataContract]
	public struct SecurityHandle :IEquatable<SecurityHandle>
	{
		public static readonly SecurityHandle Uncontrolled =
			new SecurityHandle();

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
		public override string ToString()
		{
			return base.ToString();
		}

		public static bool operator ==(
			SecurityHandle a,
			SecurityHandle b)
		{
			return Object.Equals(a, b);
		}
		public static bool operator <=(
			SecurityHandle a,
			SecurityHandle b)
		{
			return Object.Equals(a, b);
		}

		public static bool operator >=(
			SecurityHandle a,
			SecurityHandle b)
		{
			return Object.Equals(a, b);
		}

		public static bool operator !=(
			SecurityHandle a,
			SecurityHandle b)
		{
			return Object.Equals(a, b);
		}


		private string _mode;
		[DataMember(
			EmitDefaultValue = false,
			Name = "Domain",
			Order = 4)]
		string _domain;
		public string Domain
		{
			get { return _domain; }
			set { _domain = value; }
		}


		[DataMember(
			EmitDefaultValue = false,
			Name = "User",
			Order = 6)]
		string _User;
		internal string User
		{
			get { return _User; }
			set { _User = value; }
		}

		[DataMember(IsRequired = true, Name = "Mode", Order = 0)]
		public string Mode
		{
			get
			{
				return _mode;
			}
		}
		public bool IsSupervised
		{
			get
			{
				return this._mode != "U";
			}
		}

		public bool IsUnSupervised
		{
			get
			{
				return _mode == "U";
			}
		}

		public bool IsUncontrolled
		{
			get
			{
				return string.IsNullOrEmpty(_mode);
			}
		}


		internal SecurityHandle(string mode)
		{
			_mode = mode;
			_User = Environment.UserName;
			_domain = Environment.UserDomainName;
		}


		[OnDeserialized]
		private void Deserialized(StreamingContext context)
		{
			//Lock()
			//Validate()
		}

		[OnDeserializing]
		private void Deserializing(StreamingContext context)
		{
			_mode = string.Empty;
		}

		public override bool Equals(object obj)
		{
			if (obj == null || !(obj is SecurityHandle))
			{
				return false;
			}

			return Equals(this, (SecurityHandle)obj);
		}

		public bool Equals(SecurityHandle other)
		{
			return Equals(this, other);
		}

	}


}
